<?php

namespace Admin\Controller;
use Think\Controller;

class CustompromotionController extends Controller{

	/*
    列表
    */

    public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');//当前页数
        $page_size = I('post.page_size/d');

        $model=M('custom_promotion');

        $count = $model->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
        $result=$model->order("id desc")->limit($limitStart,$page_size)->select();

        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

    /*
    新增定制推广
    */
    public function add(){


        $model=D('custom_promotion');

        $corporate_namer=I('post.corporate_name');//公司名称
        $order_name=I('post.order_name');//订单名称
        $content=I('post.content');//内容简介
        $budget_amount=I('post.budget_amount');//预算
        $contacts_name=I('post.contacts_name');//联系人
        $phone=I('post.phone');//电话

        $data['budget_amount']   = $budget_amount;
        $data['phone']   = $phone;
        $data['corporate_name'] = $corporate_name;
        $data['contacts_name'] = $contacts_name;
        $data['order_name'] = $order_name;
        $data['content'] = $content;
        $data['insert_time']=time();
        $data['contacts_status']=1;//1表示查看，0表示未查看
        if($model->add($data)){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  $model->getError();
        }
        
        $this->ajaxReturn($ajaxReturn);
    }

    /*
	点击表示未联系变为已经联系
    */
    public function contacts_status(){
        $this->isAjaxLogin();//检查是否登录
    	$ids = I('post.id');

        $contacts_status = 1;

        $model = M('custom_promotion');

        $ids = explode(',',$ids);

        foreach($ids as $id){
            $model-> where('id='.$id)->setField('contacts_status',$contacts_status);
        }

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";


        $this->ajaxReturn($ajaxReturn);
    }

    /*
    详情
    */
    public  function infos(){
        $this->isAjaxLogin();

        $id = I('post.id/d');

        $Model = M('custom_promotion');

        $result = $Model->where([
                'id'=>$id,
            ])->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }
}